package com.imddy.tweb.util;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

public class ShellUtils {

	public static void execShell(String shell) {
		try {
			Runtime runtime = Runtime.getRuntime();
			runtime.exec(shell);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String listStringToString(List<String> listStr) {
		String result = "";
		for (String string : listStr) {
			result = result + string + "\n";
		}
		return result;
	}

	public static List<String> runShell(String shellStr) throws Exception {
		List<String> strList = new ArrayList<String>();

		Process process;
		process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", shellStr }, null, null);
		InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
		LineNumberReader input = new LineNumberReader(inputStreamReader);
		String line;
		process.waitFor();
		while ((line = input.readLine()) != null) {
			strList.add(line);
		}

		return strList;
	}

	
	public static List<String> runCommand(String commandStr) throws Exception {
		List<String> strList = new ArrayList<String>();

		Process process;
		process = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", commandStr }, null, null);
		InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "GBK");
		LineNumberReader input = new LineNumberReader(inputStreamReader);
		String line;
		process.waitFor();
		while ((line = input.readLine()) != null) {
			strList.add(line);
		}

		return strList;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> listString = null;
		try {
			
			// listString = runCommand("ipconfig /all");
			listString = runCommand("ping www.163.com");
			for (String string : listString) {
				System.out.println(" > " + string);
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("----------");
		System.out.println(listStringToString(listString));

	}

}
